const wxDiscode = require("../Common/wxParse/wxDiscode")
const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
// pages/recipes/recipes.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isRefresher: false,
    categorylist: [],
    selectedCategory: null,
    parentId: '',
    page: 1,
    pageSize: 10,
    keyword: '',
    recipelist: [],
    lastPage: false
  },

  refresh(e) {
    this.setData({
      page: 1,
      recipelist: []
    })
    this.getRecipelist()
  },

  load(e) {
    let page = this.data.page + 1
    this.setData({
      page
    })
    this.getRecipelist()
  },

  //搜索
  submit() {
    this.setData({
      page: 1,
      recipelist: []
    })
    this.getRecipelist()
  },

  //输入
  input(e) {
    this.setData({
      keyword: e.detail.value
    });
  },

  //获取分类列表
  getCategorylist() {
    Controller.categorylist({
      source: 6,
      parentId: this.data.parentId
    }).then(res => {
      if (res.code == 0) {
        let categorylist = res.data.baseData
        let selectedCategory = categorylist[0]
        this.setData({
          categorylist,
          selectedCategory
        })
        this.getRecipelist()
      }
    })
  },

  //获取食谱列表
  getRecipelist() {
    Controller.recipelist({
      categoryId: this.data.selectedCategory.id,
      page: this.data.page,
      pageSize: this.data.pageSize,
      keyword: this.data.keyword
    }).then(res => {
      if (res.code == 0) {
        let recipelist = this.data.recipelist
        recipelist = recipelist.concat(res.data.baseData.list)
        let lastPage = res.data.baseData.lastPage
        this.setData({
          recipelist,
          lastPage,
          isRefresher: false
        })
      }
    })
  },

  //选择分类
  selectCategory(e) {
    let index = e.currentTarget.dataset["index"]
    index = Number(index)
    let selectedCategory = this.data.categorylist[index]
    if (selectedCategory.hasChild == 1) {
      wx.navigateTo({ url: '/pages/recipes/recipes?id=' + selectedCategory.id });
      return
    }
    this.setData({
      selectedCategory,
      page: 1,
      recipelist: []
    })
    this.getRecipelist()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.title){
      wx.setNavigationBarTitle({
        title: options.title
      })
    }
    

    if (options.id) {
      this.setData({
        parentId: options.id
      })
    }
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.isRefresh) {
      this.setData({
        recipelist: []
      })
      this.getCategorylist()
    }
    app.globalData.isRefresh = true
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },

  goPage: function (e) {
    let id = e.currentTarget.dataset["id"]
    let title = e.currentTarget.dataset["title"]
    id = Number(id)
    wx.navigateTo({ url: '/pages/recipesDetails/recipesDetails?id=' + id + '&title=' + title });
  }
})